package com.lanou.dllo.soccernews.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.dllo.soccernews.R;
import com.lanou.dllo.soccernews.bean.greendao.ArticlesEntity;
import com.lanou.dllo.soccernews.utils.MyItemOnClickListener;
import com.lanou.dllo.soccernews.volley.GetVolleyDatasUtil;

import java.util.List;

/**
 * Created by dllo on 15/10/24.
 */
public class RecommendAdapter extends RecyclerView.Adapter<RecommendAdapter.MyViewHolder> {
    private List<ArticlesEntity> articlesEntities;
    private MyItemOnClickListener mItemOnClickListener;

    public void addData(List<ArticlesEntity> articlesEntities) {
        this.articlesEntities = articlesEntities;
        notifyDataSetChanged();

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.item_headlinelistview, null);

        return new MyViewHolder(view, mItemOnClickListener);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        //设置参数
        if (articlesEntities != null && articlesEntities.size() > 0) {
            holder.position = position;
            ArticlesEntity articles = articlesEntities.get(position);
            holder.ar_title.setText(articles.getTitle());

            holder.ar_discription.setText(articles.getDescription());
            holder.ar_comments_total.setText(articles.getComments_total() + "");
            //判断是否显示

            if (articles.getTop() == true) {
                holder.ar_top.setImageResource(R.drawable.news_top_draw_bg_image);
            }
            if (articles.getLabel() != null && articles.getLabel().length() > 0) {
                holder.ar_lable.setText(articles.getLabel());


                holder.ar_lable.setBackgroundColor(Color.parseColor(articles.getLabel_color()));

            }


            if (articles.getThumb() != null && articles.getThumb().length() > 0) {
                String imgUrl = articles.getThumb();
                GetVolleyDatasUtil.getNetWorkImg(imgUrl, holder.ar_thump);
            }

        }
    }

    @Override
    public int getItemCount() {
        return articlesEntities != null && articlesEntities.size() > 0 ? articlesEntities.size()
                : 0;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder implements
            View.OnClickListener {
        private TextView ar_title, ar_discription, ar_comments_total, ar_lable;

        private ImageView ar_top, ar_thump;
        private int position = 0;

        private MyItemOnClickListener mListener;

        public MyViewHolder(View itemView, MyItemOnClickListener listener) {
            super(itemView);
            //初始化
            ar_title = (TextView) itemView.findViewById(R.id.ar_title);
            ar_comments_total = (TextView) itemView.findViewById(R.id.ar_comments_total);
            ar_discription = (TextView) itemView.findViewById(R.id.ar_diseription);
            ar_lable = (TextView) itemView.findViewById(R.id.ar_lable);
            ar_top = (ImageView) itemView.findViewById(R.id.ar_top);
            ar_thump = (ImageView) itemView.findViewById(R.id.thumb);
            this.mListener = listener;
            itemView.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            if (mListener != null) {
                mListener.onItemClick(articlesEntities.get(position));
            }
        }
    }


    //设置点击事件

    public void setOnItemClickListener(MyItemOnClickListener listener) {
        this.mItemOnClickListener = listener;
    }
}
